package controller;

import model.LoadDAO;
import org.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import util.Tools;

import java.util.Calendar;
import java.util.Date;

@Controller
@RequestMapping("/shipping/loadInfo")
public class SiteLoad implements OrderAnalysis{
    @Autowired
    LoadDAO loadDAO;

    @Autowired
    Tools tools;

    @ResponseBody
    @RequestMapping(value="/getChart1", method= RequestMethod.POST, produces = "text/html;charset=utf-8")
    public String ewbCountDay(@RequestParam(value="location", required=true) String site) {
        Date end = tools.getCurrentTime(Calendar.DAY_OF_YEAR);
        if(end == null) {
            return new JSONArray().toString();
        }
        return loadDAO.ewbCount(end, Calendar.DAY_OF_YEAR, site);
    }

    @ResponseBody
    @RequestMapping(value="/getChart2", method= RequestMethod.POST, produces = "text/html;charset=utf-8")
    public String attendanceDay(@RequestParam(value="location", required=true) String site) {
        Date end = tools.getCurrentTime(Calendar.DAY_OF_YEAR);
        if(end == null) {
            return new JSONArray().toString();
        }
        return loadDAO.attendance(end, Calendar.DAY_OF_YEAR, site);
    }

    @ResponseBody
    @RequestMapping(value="/getChart3", method= RequestMethod.POST, produces = "text/html;charset=utf-8")
    public String typeWeightDay(@RequestParam(value="location", required=true) String site) {
        Date end = tools.getCurrentTime(Calendar.DAY_OF_YEAR);
        if(end == null) {
            return new JSONArray().toString();
        }
        return loadDAO.goodsType(end, Calendar.DAY_OF_YEAR, site);
    }

    @ResponseBody
    @RequestMapping(value="/getChart4", method= RequestMethod.POST, produces = "text/html;charset=utf-8")
    public String efficientDay(@RequestParam(value="location", required=true) String site) {
        Date end = tools.getCurrentTime(Calendar.DAY_OF_YEAR);
        if(end == null) {
            return new JSONArray().toString();
        }
        return loadDAO.efficient(end, Calendar.DAY_OF_YEAR, site);
    }

    @ResponseBody
    @RequestMapping(value="/getChart5", method= RequestMethod.POST, produces = "text/html;charset=utf-8")
    public String siteRelation(@RequestParam(value="location", required=true) String site) {
        Date end = tools.getCurrentTime(Calendar.DAY_OF_YEAR);
        if(end == null) {
            return new JSONArray().toString();
        }
        return loadDAO.siteRelation(end, Calendar.DAY_OF_YEAR, site);
    }

    @ResponseBody
    @RequestMapping(value="/getChart6", method= RequestMethod.POST, produces = "text/html;charset=utf-8")
    public String ewbCountHour(@RequestParam(value="location", required=true) String site) {
        Date end = tools.getCurrentTime(Calendar.HOUR_OF_DAY);
        if(end == null) {
            return new JSONArray().toString();
        }
        return loadDAO.ewbCount(end, Calendar.HOUR_OF_DAY, site);
    }

    @ResponseBody
    @RequestMapping(value="/getChart7", method= RequestMethod.POST, produces = "text/html;charset=utf-8")
    public String attendanceHour(@RequestParam(value="location", required=true) String site) {
        Date end = tools.getCurrentTime(Calendar.HOUR_OF_DAY);
        if(end == null) {
            return new JSONArray().toString();
        }
        return loadDAO.attendance(end, Calendar.HOUR_OF_DAY, site);
    }

    @ResponseBody
    @RequestMapping(value="/getChart8", method= RequestMethod.POST, produces = "text/html;charset=utf-8")
    public String typeWeightHour(@RequestParam(value="location", required=true) String site) {
        Date end = tools.getCurrentTime(Calendar.HOUR_OF_DAY);
        if(end == null) {
            return new JSONArray().toString();
        }
        return loadDAO.goodsType(end, Calendar.HOUR_OF_DAY, site);
    }

    @ResponseBody
    @RequestMapping(value="/getChart9", method= RequestMethod.POST, produces = "text/html;charset=utf-8")
    public String efficientHour(@RequestParam(value="location", required=true) String site) {
        Date end = tools.getCurrentTime(Calendar.HOUR_OF_DAY);
        if(end == null) {
            return new JSONArray().toString();
        }
        return loadDAO.efficient(end, Calendar.HOUR_OF_DAY, site);
    }
}
